import type { Plugin, HtmlTagDescriptor } from 'vite';

export default (): Plugin => {
  return {
    name: 'vite-plugin-eruda',
    transformIndexHtml(html, ctx) {
      if (process.env.NODE_ENV !== 'production') {
        const tags: HtmlTagDescriptor[] = [
          {
            tag: 'script',
            attrs: {
              src: `${ctx.server?.config.base ?? '/'}node_modules/eruda/eruda.js`,
            },
            injectTo: 'head',
          },
          {
            tag: 'script',
            children: 'eruda.init();',
            injectTo: 'head',
          },
        ]

        return {
          html,
          tags,
        }
      }
      else {
        return html;
      }
    },
  }
}
